package com.jackrain.nea.fc.utils;

import java.text.DecimalFormat;

public class ObjectToUtil {

	public static int getInt(Object str, int defaultValue) {
		try {
			int i = (int) (new Double((str + "").trim())).doubleValue();
			return i;
		} catch (Exception e) {
//			 e.printStackTrace();
		}
		return defaultValue;
	}

	public static long getLong(Object str, long defaultValue) {
		try {
			long i = (new Long((str + "").trim())).longValue();
			return i;
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return defaultValue;
	}

	public static double getDouble(Object str, double defaultValue) {
		try {
			double i = (new Double((str + "").trim())).doubleValue();
			return i;
		} catch (Exception e) {
			// e.printStackTrace();
		}
		return defaultValue;
	}

	public static String getString(Object str, String defaultValue) {

		try {

			if (!"null".equalsIgnoreCase(String.valueOf(str))) {
				return String.valueOf(str);
			}

		} catch (Exception e) {
			// e.printStackTrace();
		}

		return defaultValue;
	}

	public static boolean isNullOREmpty(String str) {

		if (str != null && !"".equals(str.trim()) && !"null".equalsIgnoreCase(str)) {
			return false;
		}

		return true;

	}

	/**
	 * 分的整数转换成小数
	 * 
	 * @param afee
	 * @return
	 */
	public static Double longToDouble(long afee) {

		Double double1 = Double.valueOf(String.valueOf(afee)) / 100;
		DecimalFormat fnum = new DecimalFormat("########0.00");
		String fee = fnum.format(double1);

		return Double.valueOf(fee);
	}

	/**
	 * 分的整数转换成小数
	 * 
	 * @param afee
	 * @return
	 */
	public static String longStrToDoubleStr(String afee) {

		if (afee == null) {
			return null;
		}

		Double double1 = Double.valueOf(afee) / 100;
		DecimalFormat fnum = new DecimalFormat("########0.00");
		String fee = fnum.format(double1);

		return fee;
	}

	/**
	 * 带小数的金额转成到分的整数
	 * 
	 * @param afee
	 *            0.00
	 * @return
	 */
	public static long doubleTOLong(Double afee) {

		if (afee == null) {
			afee = 0.00;
		}

		DecimalFormat fnum = new DecimalFormat("########0.00");
		String fee = fnum.format(afee);
		Double double2 = Double.valueOf(fee);
		DecimalFormat fnum1 = new DecimalFormat("########0");
		String feeStr = fnum1.format(double2 * 100);
		long totalFee = Long.valueOf(feeStr);

		return totalFee;
	}

	/**
	 * 带小数的金额转成到分的整数
	 * 
	 * @param afee
	 *            0.00
	 * @return
	 */
	public static long doubleStrTOLong(String afee) {

		if (afee == null) {
			afee = "0.00";
		}

		DecimalFormat fnum = new DecimalFormat("########0.00");
		String fee = fnum.format(Double.valueOf(afee));
		Double double2 = Double.valueOf(fee);
		DecimalFormat fnum1 = new DecimalFormat("########0");
		String feeStr = fnum1.format(double2 * 100);
		long totalFee = Long.valueOf(feeStr);

		return totalFee;
	}

	/**
	 * 带小数的金额转成到分的整数
	 * 
	 * @param afee
	 *            0.00
	 * @return
	 */
	public static String doubleStrTOLongStr(String afee) {

		if (afee == null) {
			afee = "0.00";
		}

		DecimalFormat fnum = new DecimalFormat("########0.00");
		String fee = fnum.format(Double.valueOf(afee));
		Double double2 = Double.valueOf(fee);
		DecimalFormat fnum1 = new DecimalFormat("########0");
		String feeStr = fnum1.format(double2 * 100);

		return feeStr;
	}

}
